;
(function (window, angular) {
    'use strict';
    angular.module("login.temp", ["pascalprecht.translate", "ui.router", "ui.bootstrap", "angular.filter", "YB.Widget", "Service.All", "ngSanitize", "template-widget"])
        .run(["ybConfig", function (ybConfig) {
            ybConfig.initConfig();
        }])

        .config(['$qProvider', '$locationProvider', function($qProvider, $locationProvider) {
            // 禁用默认的警告日志（可选）
            $qProvider.errorOnUnhandledRejections(false);

            // Hashbang 模式 改成 普通哈希模式
            $locationProvider.hashPrefix('');
        }])

        // 国际化
        .config(["$translateProvider", function ($translateProvider){

            $translateProvider.preferredLanguage(localStorage.getItem('lang')||'zh-CN');

            $translateProvider.useStaticFilesLoader({
                prefix: 'i18n/',
                suffix: '.json'
            });
        }])


        .controller("TempLoginCtrl", ['$scope', 'ybHttp', 'ybWidget', 'ybConfig', 'UserContextService', function ($scope, ybHttp, ybWidget, ybConfig, UserContextService) {
            $scope.sysName = window.AppData.config.otherData.sysName;

            function getAuthCode() {
                var service = "auth";
                var reqUrl = "/auth/authInfo";
                ybHttp.Get(service, reqUrl).then(function (data) {
                    if(data.code != 0){
                        ybWidget.ShowMessage(data.message);
                        return;
                    }

                    var pageUrl = getUrlParams("pageUrl")
                    if (pageUrl == '' || pageUrl == null || pageUrl == undefined) {
                        // 如果没指定页面，默认跳到首页
                        pageUrl = "/workBoard/main";
                    }

                    var user = {
                        loginName : getUrlParams("name"),
                        password : getUrlParams("pwd"),
                        authId : data.value.authId
                    };
                    fnLogin(user, pageUrl);
                })
            }

            function fnLogin(user, pageUrl) {
                var targetUrl = ybConfig.getConfig().baseUrl.TargetBaseSite + pageUrl;
                var service = "auth";
                var reqUrl = "/auth/loginWithoutVerifyCode";
                var result = ybHttp.Post(service, reqUrl, user);
                result.then(function (data) {
                    if (data.code === 0) {
                        localStorage.setItem("ScAuthC", data.value.ScAuth);
                        // 获取企业信息
                        UserContextService.GetSelfCompanyInfo().then((data) => {
                            window.location.href = targetUrl;
                        })
                        return;
                    }
                    ybWidget.ShowMessage(data.message);
                });
            };

            function getUrlParams(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }


            (function () {
                getAuthCode();
            })();
        }])
    ;
})(window, window.angular);